<?php
/**
 * Created by PhpStorm.
 * User: jswei
 * Date: 2018/4/24
 * Time: 8:54
 */

namespace app\admin\model;

use app\admin\controller\Push;
use app\common\controller\GetuiMsg;
use app\first\model\Notice;
use think\Db;

class Auth extends Base {
    public function getList($where = [], $order = '', $limit = 10, $field = '', $out = false){
        $list = $this
            ->field($field,$out)
            ->with(['member'])
            ->where($where)
            ->order($order)
            ->paginate($limit);
        $page = $list->currentPage();
        $total = $list->total();
        foreach ($list as $k => $v){
            $list[$k]['sort_asc'] = $k + 1 + ($page-1) * $limit;
            $list[$k]['sort_desc'] = $total - ($page-1) * $limit - $k;
        }
        return $list;
    }

    public function getOneById($id = 0, $field = '*'){
        $info = $this
            ->field($field)
            ->with('member')
            ->find($id);
        return $info;
    }

    public function add($data){
        $this->save($data);
    }

    public function member(){
        return $this->hasOne('member','id','mid')
            ->bind([
                'account',
                'nickname',
                'avatar'
            ]);
    }

    /**
     * 审核
     * @param $id
     * @param int $type
     * @param string $info
     * @param null $out
     * @return bool
     */
    public function check($id,$type=1,$info='',&$out=null){
        $_info = $this->find($id);
        if(!$_info){
            $out = '申请不存在';
            return false;
        }
        if($_info['status']!=1){
            $out = '申请处理了';
            return false;
        }
        $_msg = $_info['type']==1?'个人认证':'企业认证';
        if($type==2){
            if(empty($info)){
                $out = '请输入拒绝理由';
                return false;
            }
            $title= "审核失败";
            $_info->info = $info;
            $_type = 3;
            $_status = 3;
            $content = $msg = "抱歉！您的{$_msg}申请审核未通过！";
        }else{
            $_type = $_info['type'];
            $_status = 2;
            $title= "审核成功";
            $content = $msg = "恭喜您！您{$_msg}申请通过了审核！";
        }
        $_info->status = $_status;
        $_info->check_time = time();
        $member = (new Member())->find($_info['mid']);
        $member->is_auth = $_type;
        $member->auth_time = time();
        Db::startTrans();
        $result = $_info->save();
        $result1 = $member->save();
        $result2 = (new Notice())->addNew($_info['mid'],$title,$content,1);
        if($result && $result1 && $result2) {
            $out = [
                'uid'=>$_info['mid'],
                'type'=>$type,
                'sql'=>json_encode([
                    'sql1'=>$_info->getLastSql(),
                    'sql2'=>$member->getLastSql(),
                ]),
                'msg'=> $msg
            ];
            Db::commit();
            return true;
        }else{
            $out = '操作失败';
            Db::rollback();
            return false;
        }
    }

    /**
     * @param $value
     * @return array|bool|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getIdCardsAttr($value){
        if($value){
            $pic = new Pic();
            $url = $pic->getUrlsById(explode(',',$value));
            return $url;
        }else{
            return $value;
        }
    }

    /**
     * @param $value
     * @return array|bool|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getLicenceAttr($value){
        if($value){
            $pic = new Pic();
            $url = $pic->getUrlsById(explode(',',$value));
            return $url;
        }else{
            return $value;
        }
    }
}